var recorders = 0;
var lineSize = 10;
var pageSize = 1;
var currentPage = 1;
$(document).ready(function(){
	$.post("GetMessagesAllRecorders",{keyWord:""},function(data){
		recorders = data;
		$('#recorders').text(recorders);
		pageSize = parseInt(recorders / lineSize);
		if(pageSize%lineSize!=0){pageSize++;}
		$('#pageSize').text(pageSize);
		$('#currentPage').text(1);
		var i = 0;
		for(i=1;i<=pageSize;i++){
			$('#sp_page').append("<option value="+i+">"+i+"</option>");
		}
	});
	$.post("GetMessagesAll",{keyWord:"",currentPage:"1",lineSize:lineSize},function callback(data){
		var ucArray = eval(data);
		pageSize = parseInt(recorders / lineSize);
		if(recorders%lineSize!=0){pageSize++;}
		pageSize = (pageSize==0)?1:pageSize;
		$('#pageSize').text(pageSize);
		$('#currentPage').text(currentPage);
		var i = 0;
		$('#sp_page option').remove();
		for(i=1;i<=pageSize;i++){
			$('#sp_page').append("<option value="+i+">"+i+"</option>");
		}
		$('#area_showTable_tbody tr').remove();
	    if(ucArray.length!=0){
			$.each(ucArray,function(index,item){
		        var i=index+1;
		        $('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
		        });
		}else{
			$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
		}
	    if(currentPage==pageSize){
			$('#getLast').attr("disabled",true);
			$('#getNext').attr("disabled",true);
		  }else{
				$('#getLast').removeAttr("disabled");
				$('#getNext').removeAttr("disabled");
		 }
	});
	$('#getFirst').attr("disabled",true);
	$('#getPrev').attr("disabled",true);
});
function sbar(st, col) {
	st.style.backgroundColor = '#EAF4FE';
}
function cbar(st) {
	st.style.backgroundColor = '';
}
function searchA(){
	$('#getFirst').attr("disabled",true);
	$('#getPrev').attr("disabled",true);
	currentPage = 1;
	var kw = $('#area_search_name').val();
	lineSize = $('#lineSize').val();
	if(lineSize==""){lineSize=10;}
	var regular = /^\s+|\s+$/g;
	kw = kw.replace(regular,"");
	if(kw!=null&&kw!=""){
		$('#area_search_name').val(kw);
		$.post("GetMessagesAllRecorders",{keyWord:kw},function(data){
			recorders = data;
			$('#recorders').text(recorders);
		});
		$.post("GetMessagesAll",{keyWord:kw,currentPage:currentPage,lineSize:lineSize},function callback(data){
			var ucArray = eval(data);
			pageSize = parseInt(recorders / lineSize);
			if(recorders%lineSize!=0){pageSize++;}
			pageSize = (pageSize==0)?1:pageSize;
			$('#pageSize').text(pageSize);
			$('#currentPage').text(currentPage);
			var i = 0;
			$('#sp_page option').remove();
			for(i=1;i<=pageSize;i++){
				$('#sp_page').append("<option value="+i+">"+i+"</option>");
			}
			$('#area_showTable_tbody tr').remove();
	        if(ucArray.length!=0){
				$.each(ucArray,function(index,item){
		        	var i=index+1;
		        	$('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
		        });
			}else{
				$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
			}
	        if(currentPage==pageSize){
				$('#getLast').attr("disabled",true);
				$('#getNext').attr("disabled",true);
			}else{
				$('#getLast').removeAttr("disabled");
				$('#getNext').removeAttr("disabled");
		}
		});
	}else{
		alert("查询内容不能为空！");
	}
}
function searchB(){
	currentPage = 1;
	$('#area_search_name').val("");
	lineSize = $('#lineSize').val();
	if(lineSize==""){lineSize=10;}
	$.post("GetMessagesAllRecorders",{keyWord:""},function(data){
			recorders = data;
			$('#recorders').text(recorders);
		});
	$.post("GetMessagesAll",{keyWord:"",currentPage:currentPage,lineSize:lineSize},function callback(data){
		var ucArray = eval(data);
		pageSize = parseInt(recorders / lineSize);
		if(recorders%lineSize!=0){pageSize++;}
		pageSize = (pageSize==0)?1:pageSize;
		$('#pageSize').text(pageSize);
		$('#currentPage').text(currentPage);
		var i = 0;
		$('#sp_page option').remove();
		for(i=1;i<=pageSize;i++){
			$('#sp_page').append("<option value="+i+">"+i+"</option>");
		}
		$('#area_showTable_tbody tr').remove();
	    if(ucArray.length!=0){
			$.each(ucArray,function(index,item){
		        var i=index+1;
		        $('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
		        });
		}else{
			$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
		}
	    if(currentPage==pageSize){
			$('#getLast').attr("disabled",true);
			$('#getNext').attr("disabled",true);
		}else{
			$('#getLast').removeAttr("disabled");
			$('#getNext').removeAttr("disabled");
		}
	    $('#getFirst').attr("disabled",true);
		$('#getPrev').attr("disabled",true);
	});
}
function getFirst(){
	currentPage = 1;
	var kw = $('#area_search_name').val();
	var regular = /^\s+|\s+$/g;
	kw = kw.replace(regular,"");
	$.post("GetMessagesAllRecorders",{keyWord:kw},function(data){
		recorders = data;
		$('#recorders').text(recorders);
	});
	$.post("GetMessagesAll",{keyWord:kw,currentPage:currentPage,lineSize:lineSize},function callback(data){
		var ucArray = eval(data);
		pageSize = parseInt(recorders / lineSize);
		if(recorders%lineSize!=0){pageSize++;}
		pageSize = (pageSize==0)?1:pageSize;
		$('#pageSize').text(pageSize);
		$('#currentPage').text(1);
		$('#sp_page option[value='+currentPage+']').attr("selected",true);
		$('#area_showTable_tbody tr').remove();
	    if(ucArray.length!=0){
			$.each(ucArray,function(index,item){
		       var i=index+1;
		       $('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
		       });
		}else{
			$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
		}
	});
	
	if(currentPage==pageSize){
		$('#getLast').attr("disabled",true);
		$('#getNext').attr("disabled",true);
	}else{
		$('#getLast').removeAttr("disabled");
		$('#getNext').removeAttr("disabled");
	}
	$('#getFirst').attr("disabled",true);
	$('#getPrev').attr("disabled",true);
}
function getPrev(){
	var kw = $('#area_search_name').val();
	var regular = /^\s+|\s+$/g;
	kw = kw.replace(regular,"");
	currentPage = currentPage - 1;
	if(currentPage>=1){
		$('#currentPage').text(currentPage);
		$.post("GetMessagesAll",{keyWord:kw,currentPage:currentPage,lineSize:lineSize},function callback(data){
			var ucArray = eval(data);
			$('#sp_page option[value='+currentPage+']').attr("selected",true);
			$('#area_showTable_tbody tr').remove();
		    if(ucArray.length!=0){
				$.each(ucArray,function(index,item){
			       var i=index+1;
			       $('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
			       });
			}else{
				$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
			}
		});
	}
	if(currentPage==pageSize){
		$('#getLast').attr("disabled",true);
		$('#getNext').attr("disabled",true);
	}else{
		$('#getLast').removeAttr("disabled");
		$('#getNext').removeAttr("disabled");
	}
	if(currentPage==1){
		$('#getPrev').attr("disabled",true);
		$('#getFirst').attr("disabled",true);
	}else{
		$('#getPrev').removeAttr("disabled");
		$('#getFirst').removeAttr("disabled");
	}
}
function getNext(){
	var kw = $('#area_search_name').val();
	var regular = /^\s+|\s+$/g;
	kw = kw.replace(regular,"");
	currentPage = currentPage + 1;
	if(currentPage<=pageSize){
		$('#currentPage').text(currentPage);
		$.post("GetMessagesAll",{keyWord:kw,currentPage:currentPage,lineSize:lineSize},function callback(data){
			var ucArray = eval(data);
			$('#sp_page option[value='+currentPage+']').attr("selected",true);
			$('#area_showTable_tbody tr').remove();
		    if(ucArray.length!=0){
				$.each(ucArray,function(index,item){
			       var i=index+1;
			       $('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
			       });
			}else{
				$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
			}
		});
	}
	if(currentPage==1){
		$('#getFirst').attr("disabled",true);
		$('#getPrev').attr("disabled",true);
	}else{
		$('#getFirst').removeAttr("disabled");
		$('#getPrev').removeAttr("disabled");
	}
	if(currentPage==pageSize){
		$('#getLast').attr("disabled",true);
		$('#getNext').attr("disabled",true);
	}else{
		$('#getLast').removeAttr("disabled");
		$('#getNext').removeAttr("disabled");
	}
}
function getLast(){
	var kw = $('#area_search_name').val();
	var regular = /^\s+|\s+$/g;
	kw = kw.replace(regular,"");
	$.post("GetMessagesAllRecorders",{keyWord:kw},function(data){
		recorders = data;
		$('#recorders').text(recorders);
	});
	pageSize = parseInt(recorders / lineSize);
	if(recorders%lineSize!=0){pageSize++;}
	pageSize = (pageSize==0)?1:pageSize;
	$('#pageSize').text(pageSize);
	$('#currentPage').text(pageSize);
	currentPage = pageSize;
	$('#getLast').attr("disabled",true);
	$.post("GetMessagesAll",{keyWord:kw,currentPage:pageSize,lineSize:lineSize},function callback(data){
		var ucArray = eval(data);
		$('#sp_page option[value='+currentPage+']').attr("selected",true);
		$('#area_showTable_tbody tr').remove();
	    if(ucArray.length!=0){
			$.each(ucArray,function(index,item){
		       var i=index+1;
		       $('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
		       });
		}else{
			$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
		}
	});
	if(currentPage==1){
		$('#getFirst').attr("disabled",true);
		$('#getPrev').attr("disabled",true);
	}else{
		$('#getFirst').removeAttr("disabled");
		$('#getPrev').removeAttr("disabled");
	}
	$('#getLast').attr("disabled",true);
	$('#getNext').attr("disabled",true);
}
function getSomePage(){
	$('#sp_page option[selected=selected]').removeAttr("selected");
	var kw = $('#area_search_name').val();
	var regular = /^\s+|\s+$/g;
	kw = kw.replace(regular,"");
	currentPage = $('#sp_page').val();
	currentPage = parseInt(currentPage);
	$('#sp_page option[value='+currentPage+']').attr("selected",true);
	$('#currentPage').text(currentPage);
	$.post("GetMessagesAll",{keyWord:kw,currentPage:currentPage,lineSize:lineSize},function callback(data){
		var ucArray = eval(data);
		$('#area_showTable_tbody tr').remove();
	    if(ucArray.length!=0){
			$.each(ucArray,function(index,item){
		       var i=index+1;
		       $('#area_showTable_tbody').append("<tr onmouseover=sbar(this) onmouseout=cbar(this)><td>"+i+"</td><td><a href=home/message_view.jsp?id="+item.id+">"+item.title
		        				+"</a></td><td>"+item.date+"</td><td>"+item.from
		        				+"</td></tr>");
		       });
		}else{
			$('#area_showTable_tbody').append("<tr><td colspan=5>暂时没有任何数据!</td></tr>");
		}
	});
	if(currentPage==1){
		$('#getFirst').attr("disabled",true);
		$('#getPrev').attr("disabled",true);
	}else{
		$('#getFirst').removeAttr("disabled");
		$('#getPrev').removeAttr("disabled");
	}
	if(currentPage==pageSize){
		$('#getLast').attr("disabled",true);
		$('#getNext').attr("disabled",true);
	}else{
		$('#getLast').removeAttr("disabled");
		$('#getNext').removeAttr("disabled");
	}
}